package com.aier.common.action;

import java.io.File;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.ImageUtil;

@Scope("prototype")
@Component("commonImageAction")
public class CommonImageAction extends CommonBaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    // 上传的图片
    private File selectPhoto;
    // 上传图片的名称或路径
    private String photoName;
    // 图片保存路径
    private String imageUrl;
    // 返回图片名称
    private String imageName;
    // 图片是否需要压缩(0不需要 1需要)
    private Integer type;
    
    // 错误码
    private int s;

    /**
     * 上传图片
     * @return
     */
    public String uploadImage() {
        imageName = ImageUtil.uploadImage(selectPhoto, imageUrl, photoName,type);
        if ("".equals(imageName) || imageName == null) {
            s = 0;
        } else {
            s = 1;
        }
        
        return SUCCESS;
    }
    
   /**
    * 删除图片
    * @return
    */
    public String deleteImage() {
        File fo1 = new File(imageUrl + "big/" + photoName);
        File fo2 = new File(imageUrl + "origin/" + photoName);
        File fo3 = new File(imageUrl + "small/" + photoName);
        if (fo1.exists()) {
            fo1.delete();
        }
        if (fo2.exists()) {
            fo2.delete();
        }
        if (fo3.exists()) {
            fo3.delete();
        }
        s = 1;
        return SUCCESS;
    }

    public String getImageName() {
        return imageName;
    }

    public int getS() {
        return s;
    }

    public void setSelectPhoto(File selectPhoto) {
        this.selectPhoto = selectPhoto;
    }

    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    
}
